package com.sd.repay.service.impl;

import com.sd.repay.common.Constants;
import com.sd.repay.common.Page;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.ExtractionOrder;
import com.sd.repay.service.ExtractOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author sd
 * @Description: 提现表Service接口实现类
 * @date 2017年10月25日
 */
@Service
public class ExtractOrderServiceImpl implements ExtractOrderService {

    @Autowired
    private BaseDao baseDao;

    /**
     * 新增提现记录
     *
     * @param extOrder
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int saveExtractOrder(ExtractionOrder extOrder) throws Exception {

        String sql = " INSERT INTO t_extraction_order(order_no,mer_type,mer_no,acq_code,card_no,account_no,account_name,mobile_no,id_card_no,amount,fee,status,result_code," +
                "result_msg,order_data,create_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(extOrder.getOrderNo());
        params.add(extOrder.getMerType());
        params.add(extOrder.getMerNo());
        params.add(extOrder.getAcqCode());
        params.add(extOrder.getCardNo());
        params.add(extOrder.getAccountNo());
        params.add(extOrder.getAccountName());
        params.add(extOrder.getMobileNo());
        params.add(extOrder.getIdCardNo());
        params.add(extOrder.getAmount());
        params.add(extOrder.getFee());
        params.add(Constants.EXTRACT_STATUS_INIT);
        params.add(extOrder.getResultCode());
        params.add(extOrder.getResultMsg());
        params.add(extOrder.getOrderData());
        params.add(new Date());
        return baseDao.execute(sql, params.toArray());
    }

    /**
     * 根据订单号获取提现记录
     *
     * @param orderNo
     * @return
     * @throws Exception
     */
    @Override
    public ExtractionOrder getExtractByOrderNo(String orderNo) throws Exception {

        String sql = " SELECT * FROM t_extraction_order WHERE order_no = ? ";
        return baseDao.findFirst(ExtractionOrder.class, sql, orderNo);
    }

    /**
     * 修改提现记录状态
     *
     * @param orderNo
     * @param newStatus
     * @param oldStatus
     * @param isReverse
     * @param resultCode
     * @param resultMsg
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int updateExtractStatus(String orderNo, String newStatus, String oldStatus, String isReverse, String reverseStatus, String resultCode, String resultMsg) throws Exception {

        String sql = " UPDATE t_extraction_order SET status = ?, is_reverse = ?, reverse_status = ?, result_code = ?, result_msg = ? WHERE order_no = ? ";
        List<Object> params = new ArrayList<>();
        params.add(newStatus);
        params.add(isReverse);
        params.add(reverseStatus);
        params.add(resultCode);
        params.add(resultMsg);
        params.add(orderNo);

        if (StringUtils.isNotBlank(oldStatus)) {
            sql += " AND status = ? ";
            params.add(oldStatus);
        }
        return baseDao.execute(sql, params.toArray());
    }

    /**
     * 修改提现冲正状态
     *
     * @param orderNo
     * @param newReverseStatus
     * @param oldReverseStatus
     * @param resultCode
     * @param resultMsg
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int updateExtractReverseStatus(String orderNo, String newReverseStatus, String oldReverseStatus, String resultCode, String resultMsg) throws Exception {

        String sql = " UPDATE t_extraction_order SET reverse_status = ?, result_code = ?, result_msg = ? WHERE order_no = ? AND is_reverse = ? ";
        List<Object> params = new ArrayList<>();
        params.add(newReverseStatus);
        params.add(resultCode);
        params.add(resultMsg);
        params.add(orderNo);
        params.add(Constants.EXTRACT_IS_REVERSE);

        if (StringUtils.isNotBlank(oldReverseStatus)) {
            sql += " AND reverse_status = ? ";
            params.add(oldReverseStatus);
        }
        return baseDao.execute(sql, params.toArray());
    }

    /**
     * 查询需要30天内10分钟前创建的需要冲正的提现订单记录
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<ExtractionOrder> getReverseExtractOrder() throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();

        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        nowCal.add(Calendar.DAY_OF_YEAR, -30);
        String beginTime = sdf.format(nowCal.getTime()) + " 00:00:00";

        nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        nowCal.add(Calendar.MINUTE, -10);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format(nowCal.getTime());

        String sql = " SELECT * FROM t_extraction_order WHERE is_reverse = ? AND (reverse_status <> ? OR reverse_status IS NULL) AND create_time >= ? AND create_time < ? ";
        return baseDao.findList(ExtractionOrder.class, sql, new Object[]{Constants.EXTRACT_IS_REVERSE, Constants.EXTRACT_REVERSE_SUCCESS, beginTime, endTime});
    }

    /**
     * 分页查询提现记录
     *
     * @param merType
     * @param merNo
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Page<ExtractionOrder> getExtractionOrderPageByMer(String merType, String merNo, String startTime, String endTime, int pageNo, int pageSize) throws Exception {
        String sql = " SELECT * FROM t_extraction_order WHERE mer_type = ? AND mer_no = ? and create_time between ? and ?  ";
        return baseDao.queryPagination(ExtractionOrder.class, pageNo, pageSize, sql, merType, merNo, startTime, endTime);
    }
}
